
$("#saveAs").click(function () {
    var content = $(".jsonformatter-textarea").eq(0).val();
    var blob = new Blob([content], { type: "text/plain;charset=utf-8" });
    saveAs(blob, "content.json");
});

try {

    app.load();
    app.resize();

    $(function () {
        $(document).keydown(function (e) {
            if (e.keyCode == 115) {
                var leftval = $('.jsonformatter-textarea').val();
                if (leftval == "") return;
                if (leftval.substring(0, 1) == "{" || leftval.substring(0, 1) == "[") {
                    return;
                }
                var startPosition = leftval.indexOf("{");

                leftval = leftval.substring(startPosition);
                var lastPosition = leftval.lastIndexOf("}");
                leftval = leftval.substring(0, lastPosition + 1);
                $('.jsonformatter-textarea').val(leftval);
            }
        });
    })

    const $jsonTextArea = $(".jsonformatter-textarea");
    utools.onPluginEnter(({ code, type, payload }) => {
        $jsonTextArea.focus();
        $jsonTextArea.val(payload);
        app.formatterToEditor();
        // 	console.log(payload);
        // 	var json = JSONEditor.parse(payload);
        // console.log(json);
    });
}
catch (e) {
}



//--------------------------------------------------------------------------
const $jsonTextArea = $(".jsonformatter-textarea");
$("#testcase").click(function () {
    $jsonTextArea.val('[\n\t{"id":"1","title":"BeJSON","url":"www.bejson.com"},\n\t{"id":"2","title":"layui","url":"www.layui.com"}\n]');
});

var fgf = ",";//分隔符
function ctyperow() {
    fgf = $("#fgfstr").val() || fgf;
    var instr = $jsonTextArea.val();
    var jsons = JSON.parse(instr);
    if (jsons.length < 1) {
        alert("数组小于一行数据"); return;
    }
    var titles = new Array();
    for (var key in jsons[0]) {
        titles.push(key);
    }

    var values = new Array();
    for (var i = 0, l = jsons.length; i < l; i++) {
        var value = new Array();
        for (var key in jsons[i]) {

            value.push(jsons[i][key]);
        }
        values.push(value);
    }

    var html = '';
    html += titles.join(fgf) + "\n"
    for (var i = 0; i < values.length; i++) {
        html += values[i].join(fgf) + "\n";
    }
    $("#content").val(html);
}
//下载 csv数据
$("#download").click(function () {
    var content = $('#content').val();
    var blob = new Blob([content], { type: "text/plain;charset=utf-8" });
    saveAs(blob, "content.csv");
});

//转换为 csv数据
$("#changebtn").click(function () {
    ctyperow(true);
});

function deser() {
    $.ajax({
        type: "POST", //GET或POST,
        async: true, //默认设置为true，所有请求均为异步请求。
        url: "http://www.bejson.com/convert/ajax/deserialize/",
        data: {
            content: $('#content').val(),
        },
        dataType: "json", //xml、html、script、jsonp、text
        beforeSend: function () { },
        complete: function () { },
        success: function (data) {
            $jsonTextArea.val(JSON.stringify(JSON.parse(data.json1)));
            $("#phpout").val(data.dump);
        }
    });
}
function demo2() {
    document.getElementById('content').value = 'a:3:{s:4:"name";s:6:"huqian";s:3:"sex";s:3:"男";s:3:"age";i:27;}';
}
function demo() {
    document.getElementById('content').value = '{"adr":"详细地址a","tenant_num_id":6,"town_num_id":"","city_area_num_id":110101,"client_list":[{"cont_empe":"联系人1","telephone":"18201919191","postcode":"22","cont_empe_fax":"3","cont_empe_mail":"4","index":0}],"collect_num_id":"","loc_list":["aaa","ccccc","bbbbb"],"sub_unit_name":"门店名称1","type_num_id":"3"}';
}
function Empty() {
    $jsonTextArea.val('');
    document.getElementById('out').value = '';
    document.getElementById('phpout').value = '';
    $jsonTextArea.select();
}

function GetFocus() {
    document.getElementById('content').focus();
}